package com.banciyuan.bcywebview.utils.file;

import android.content.Context;
import android.util.Log;

import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.download.BaseImageDownloader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by squall on 2015/10/27.
 */
public class ByteUtils {

    private static int Buffer_Size = 6 * 1024;

    public static byte[] parseImageToByte(File mfile) throws IOException {

        FileInputStream inputStream = new FileInputStream(mfile.getAbsoluteFile());
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[Buffer_Size];
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, Buffer_Size)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        String numString = HttpUtils.ENCODENUM;
        int num = Integer.parseInt(numString);
        for (int i = 0; i < num; i++) {
            in2b[i] = (byte) -in2b[i];
        }
        swapStream.close();
        inputStream.close();
        return in2b;
    }

    public static byte[] parseImageToByteSave(InputStream inputStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[Buffer_Size];
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, Buffer_Size)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        if (!StringUtil.notNullEqual(new FileTypeUtil().getFileTypeByStream(in2b), "gif")) {
            String numString = HttpUtils.ENCODENUM;
            int num = Integer.parseInt(numString);
            for (int i = 0; i < num; i++) {
                in2b[i] = (byte) -in2b[i];
            }
        }
        swapStream.close();
        inputStream.close();
        return in2b;
    }

    public static byte[] parseImageToByteGet(InputStream inputStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[Buffer_Size];
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, Buffer_Size)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        String numString = HttpUtils.ENCODENUM;
        int num = Integer.parseInt(numString);
        for (int i = 0; i < num; i++) {
            in2b[i] = (byte) -in2b[i];
        }
        swapStream.close();
        inputStream.close();
        return in2b;
    }

    public static String parseEncodeFileToDecode(Context context, File orgin, String tempPath) {
        try {
            BaseImageDownloader baseImageDownloader = new BaseImageDownloader(context);
            InputStream is = baseImageDownloader.getStream("file://" + orgin.getAbsolutePath(), null, true);
            File tempDir = new File(tempPath);
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            File tempFile = new File(tempPath + orgin.getName());
            if (!tempFile.exists()) {
                tempFile.createNewFile();
                OutputStream os = new FileOutputStream(tempFile, true);
                byte[] buff = new byte[Buffer_Size];
                int rc = 0;
                while ((rc = is.read(buff, 0, Buffer_Size)) > 0) {
                    os.write(buff, 0, rc);
                }
                os.close();
            }
            return tempFile.getAbsolutePath();

        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
        return "error";
    }

}
